package com.hibernate.persistence.lifecycle;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class QueryEntity {

	private static SessionFactory factory = new Configuration().configure().buildSessionFactory();
	private static Session session;

	public static void main(String[] args) {
		// QueryEntity
		session = factory.openSession();
		// Query q = session.createQuery("from Message");
		// Query q = session.createQuery("from Message where id > :id");
		Query q = session.createQuery("from Message where id >= :id");
		q.setParameter("id", 4);
		List l = q.list();

		for (Object obj : l) {
			Message msg = (Message) obj;
			Serializable id = session.getIdentifier(msg);
			System.out.println("Id=" + id + ", msgText=" + msg.toString());
		}

		if (l.size() > 0) System.out.println("List size=" + l.size());
		else System.out.println("Empty List");

		session.close();

	}
}
